import Taro, { Component } from '@tarojs/taro'
import { View , Button } from '@tarojs/components';
import NoLogin from '../../components/nologin/nologin';
import './tablist.scss';
import UserList from '../../components/usercarlist/usercarlist';
import CarList from  '../../components/drivercarlist/drivercarlist';
import { getToken , getRole} from '../../util/globaldata';
import { getUserOrderStatus } from '../../util/api';
export default class TabList extends Component {
    config = {
        navigationBarTitleText:"",
    }
    constructor(){
        super(...arguments);
        this.state = {
            userRole:1,
            hasOrder:false,
            isLogin:false,
            pageShow:false,
            orderCode:"",
        }
    }
    config = {
        navigationBarTitleText:'Free出行',
        enablePullDownRefresh:true,
        backgroundTextStyle: "dark"
    }
    onPullDownRefresh() {
        Taro.eventCenter.trigger("loopRefresh")
    }
    componentDidShow(){
        this.setState({
            isLogin:getToken() != null
        })
        Taro.setNavigationBarTitle({
            title:getRole() == 1 ? '司机列表' : '车主列表',
        })
        getUserOrderStatus({},true).then( res => {
            this.setState({hasOrder:res.code != 1,userRole:res.data,orderCode:res.code},() => {
                this.setState({pageShow:true}) 
                this.eventLoopStart();
            });
        })
    }
    
    eventLoopStart(){ //列表轮训开始
        let { hasOrder } = this.state;
        if(hasOrder){
            this.timp ? clearInterval(this.timp) : ""
            this.timp = setInterval(() => {
                Taro.eventCenter.trigger("loopRefresh");
                getUserOrderStatus({},true).then( res => {
                    this.setState({hasOrder:res.code != 1,userRole:res.data,orderCode:res.code},() => {
                        this.setState({pageShow:true}) 
                    });
                })
            },1000 * 15)
        }
    }
    toOrderCenter(){
        let { orderCode } = this.state;
        let type = 1;
        if(orderCode == 0){
            type = 1;
        }else if(orderCode == 2){
            type = 2;
        }else if(orderCode == 3){
            type = 3;
        }
        Taro.navigateTo({
            url:"/pages/orderlist/orderlist?type="+type
        })
    }
    render(){
        let { userRole , isLogin , orderCode } = this.state;
        return (
            <View className='tablist'>
                {
                    pageShow ?  isLogin ? (
                        <View className='allogin'>
                            {
                                hasOrder ? (orderCode == 2 || orderCode == 3) ? (<View className='order'><View className='order-txt' onClick={this.toOrderCenter.bind(this)}>您有订单正在进行</View><Button className='order-btn' onClick={this.toOrderCenter.bind(this)}>订单中心</Button></View>) :  (userRole == 1 ?  <UserList /> : <CarList />) : (<View className='relese-order flex flex-wrap align-content-center'><View className='txt'>您还没有发布订单</View><View className='txt'>发布订单后可查看大厅内容</View></View>)
                            }
                        </View>
                    ) : <NoLogin /> : ''
                }
            </View>
        )
   }
}
